package com.i2works.smartluolong.utils.common;

import com.google.common.collect.Maps;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/6/12 17:11
 */

public class RequestUtils {

    /**
     * 返回值类型为Map<String, Object>
     */
    public static LinkedHashMap<String, Object> getParameterMap(HttpServletRequest request) {
        //把请求参数封装到Map<String, String[]>中
        Map<String, String[]> properties = request.getParameterMap();
        LinkedHashMap<String, Object> returnMap = Maps.newLinkedHashMap();
        Iterator<Map.Entry<String, String[]>> iter = properties.entrySet().iterator();
        String name = "";
        String value = "";
        while (iter.hasNext()) {
            Map.Entry<String, String[]> entry = iter.next();
            name = entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else {
                String[] values = (String[]) valueObj;
                //用于请求参数中有多个相同名称
                for (String value1 : values) {
                    value = value1 + ",";
                }
                value = value.substring(0, value.length() - 1);
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }

}
